package com.lxkpt.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lxkpt.bean.Business;
import com.lxkpt.bean.User;
import com.lxkpt.service.BusinessService;
import com.lxkpt.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/userManage")
public class UserManageController {

    Map<Object, Object> map = new HashMap<>();

    @Resource
    private UserService userService;

    @Resource
    private BusinessService businessService;

    /**
     * 根据userName查询单个用户
     *
     * @param userName
     * @return
     */
    @RequestMapping("/getUserByUserName")
    @RequiresRoles("admin")
    @ResponseBody
    public Object getUserByUserName(@RequestParam(value = "userName") String userName) {
        User u = new User();
        u.setUserName(userName);
        User user = userService.selectOneUser(u);
        if (user != null) {
            map.put("user", user);
        }
        return map;
    }

    /**
     * 检查用户名
     *
     * @param userName
     * @return
     */
    @RequestMapping("/checkUserName")
    @RequiresRoles("admin")
    @ResponseBody
    public Object checkUserName(@RequestParam(value = "userName") String userName) {
        if (userName != null) {
            User user = new User();
            user.setUserName(userName);
            User result = userService.selectOneUser(user);
            if (result == null) {
                map.put("result", "success");
            }
        }
        return map;
    }


    @RequestMapping(value = "/addUser", method = RequestMethod.GET)
    @RequiresRoles("admin")
    public String addUser() {
        return "user-add";
    }

    /**
     * 新增用户
     *
     * @return
     */
    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    @RequiresRoles("admin")
    @ResponseBody
    public Object searchUser(User user) {
        Md5Hash md5UserPassword = new Md5Hash(user.getUserPassword(), user.getUserName(), 3);
        user.setUserPassword(md5UserPassword.toString());
        user.setUserLevelId(3);
        int result = userService.insertSelective(user);
        if (result > 0) {
            map.put("result", "success");
        }
        return map;
    }

    /**
     * 搜索用户
     *
     * @param pn
     * @param type
     * @param content
     * @param model
     * @return
     */
    @RequestMapping("/searchUser")
    @RequiresRoles("admin")
    public String searchUser(@RequestParam(value = "pn", defaultValue = "1") String pn,
                             @RequestParam(value = "type") String type,
                             @RequestParam(value = "content") String content,
                             Model model) {
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        content = content.trim().replaceAll(" ", "");
        User user = new User();
        List<User> userList = null;
        PageHelper.startPage(pageNo, 10);
        switch (type) {
            case "userName":
                user.setUserName(content);
                userList = userService.searchUser(user);
                break;
            case "userPhone":
                user.setUserPhone(content);
                userList = userService.searchUser(user);
                break;
            case "userEmail":
                user.setUserEmail(content);
                userList = userService.searchUser(user);
                break;
            default:
                break;
        }
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        model.addAttribute("pageInfo", pageInfo);
        return "user-search";
    }

    /**
     * 更新用户
     *
     * @param user
     * @return
     */
    @RequestMapping("/updateUser")
    @RequiresRoles("admin")
    @ResponseBody
    public Object updateUser(User user) {
        Md5Hash md5UserPassword = new Md5Hash(user.getUserPassword(), user.getUserName(), 3);
        user.setUserPassword(md5UserPassword.toString());
        int result = userService.updateByPrimaryKeySelective(user);
        if (result > 0) {
            map.put("result", "success");
        }
        return map;
    }

    /**
     * 获得用户
     *
     * @param userId
     * @return
     */
    @RequestMapping("/getUser")
    @RequiresRoles("admin")
    public Object getUser(@RequestParam(value = "userId") int userId, Model model) {
        User u = new User();
        u.setUserId(userId);
        User user = userService.selectOneUser(u);
        model.addAttribute("user", user);
        return "user-update";
    }

    /**
     * 删除用户
     *
     * @param userId
     * @return
     */
    @RequestMapping("/deleteUser")
    @RequiresRoles("admin")
    @ResponseBody
    public Object deleteUser(@RequestParam(value = "userId") int userId) {
        int result = userService.deleteByPrimaryKey(userId);
        Business bn = new Business();
        bn.setBnAuthorId(userId);
        List<Business> businessList = businessService.selectBusiness(bn);
        if (businessList.size() > 0) {
            for (Business b: businessList) {
                businessService.deleteByPrimaryKey(b.getBnId());
            }
        }
        if (result > 0) {
            map.put("result", "success");
        }
        return map;
    }

    /**
     * 用户列表
     *
     * @return
     */
    @RequestMapping("/userList")
    @RequiresRoles("admin")
    public String userList(@RequestParam(value = "pn", defaultValue = "1") String pn, Model model) {
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        PageHelper.startPage(pageNo, 15);
        User user = new User();
        user.setUserLevelId(3);
        List<User> userList = userService.selectUser(user);
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        model.addAttribute("pageInfo", pageInfo);
        return "user-manage";
    }
}
